package com.xju.admin.service.Impl;

import com.xju.admin.entity.*;
import com.xju.admin.mapper.ExamMapper;
import com.xju.admin.service.DictionaryService;
import com.xju.admin.service.ExamService;
import com.xju.admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExamServiceImpl implements ExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private UserService userService;

    @Override
    public ExamResult findAll() {
        Dictionary status=dictionaryService.findByTypeAndValue("试卷状态","可用");
        ExamExample examExample=new ExamExample();
        examExample.createCriteria().andExamStatusEqualTo(status.getDicId());
        List<Exam> examList=examMapper.selectByExampleWithBLOBs(examExample);
        List<ExamShow> examShowsList=new ArrayList<>();
        for (Exam exam:examList){
            Dictionary dictionary=dictionaryService.findById(exam.getExamStatus());
            ExamShow examShow=new ExamShow(exam);
            examShow.setNewStatus(dictionary.getDicValue());
            User user=userService.findById(exam.getExamCreator());
            examShow.setNewCreator(user.getUserName());

            examShowsList.add(examShow);
        }
        List<Dictionary> dictionaryList=dictionaryService.findByType("试卷状态");
        ExamResult examResult=new ExamResult(examShowsList,dictionaryList);
        return examResult;
    }

    @Override
    public void delete(String id) {
        examMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ExamResult review() {
        Dictionary status=dictionaryService.findByTypeAndValue("试卷状态","待审核");
        ExamExample examExample=new ExamExample();
        examExample.createCriteria().andExamStatusEqualTo(status.getDicId());
        List<Exam> examList=examMapper.selectByExampleWithBLOBs(examExample);
        List<ExamShow> examShowsList=new ArrayList<>();
        for (Exam exam:examList){
            Dictionary dictionary=dictionaryService.findById(exam.getExamStatus());
            ExamShow examShow=new ExamShow(exam);
            examShow.setNewStatus(dictionary.getDicValue());
            User user=userService.findById(exam.getExamCreator());
            examShow.setNewCreator(user.getUserName());
            examShowsList.add(examShow);
        }
        List<Dictionary> dictionaryList=dictionaryService.findByType("试卷状态");
        ExamResult examResult=new ExamResult(examShowsList,dictionaryList);
        return examResult;
    }

    @Override
    public void pass(String id) {
        ExamExample examExample=new ExamExample();
        examExample.createCriteria().andExamIdEqualTo(id);
        Exam exam=examMapper.selectByExampleWithBLOBs(examExample).get(0);
        Dictionary dictionary=dictionaryService.findByTypeAndValue("试卷状态","可用");
        exam.setExamStatus(dictionary.getDicId());
        examMapper.updateByPrimaryKeyWithBLOBs(exam);
    }
}
